<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Model\LinkModel;
use Illuminate\Http\Request;

class LinkController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //链接首页
        $linkModel = new LinkModel;
        $links = $linkModel->getLinks();
        return view('admin.link.index',['links' => $links]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //添加页面
        return view('admin.link.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //添加链接
        $data = $request->except('_token');
        $linkModel = new LinkModel;
        $res = $linkModel->insertLink($data);

        if ( $res ) {
            return ['msg' => '添加成功' , 'status' => 'success'];
        } else {
            return ['msg' => '添加失败' , 'status' => 'fail'];
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

/**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //修改页面
        $linkModel = new LinkModel;
        $link = $linkModel->getLink($id);

        return view('admin.link.edit')->with('link' , $link);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //更新
        $data = $request->except('_token','_method');
        $linkModel = new LinkModel;
        $res = $linkModel->upLink($id , $data);

        if ( $res ) {
            return ['msg' => '更新成功' , 'status' => 'success'];
        } else {
            return ['msg' => '更新失败' , 'status' => 'fail'];
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //删除
        $linkModel = new LinkModel;
        $res = $linkModel->delLink($id);

        if ( $res ) {
            return ['msg' => '删除成功' , 'status' => 'success'];
        } else {
            return ['msg' => '删除失败' , 'status' => 'fail'];
        }
    }
}
